//
//  ChannelTableViewCell.m
//  ShrimpCrowdordering
//
//  Created by 郑洲 on 2018/7/25.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "ChannelTableViewCell.h"

@interface ChannelTableViewCell ()

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *noteLabel;
@property (nonatomic, strong) UIView  *tagView;
@property (nonatomic, strong) UILabel *tagLabel;
@property (nonatomic, strong) UIButton *concernBtn;
@property (nonatomic, strong) UIImageView *headImage;
@property (nonatomic, strong) UILabel *numLabel;
@property (nonatomic, strong) UIButton *videoBtn;

@end

@implementation ChannelTableViewCell

+ (ChannelTableViewCell *)cellWithTableView:(UITableView *)tableView andIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellID = @"ChannelTableViewCell";
    ChannelTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[ChannelTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell createSubView];
    }
    
    return cell;
}

- (void)createSubView {
    UIView *shadowBg = [[UIView alloc] initWithFrame:CGRectMake(ScreenX375(19), ScreenX375(12), (SCREEN_WIDTH - ScreenX375(38)), ScreenX375(407))];
    shadowBg.backgroundColor = [UIColor whiteColor];
    shadowBg.layer.cornerRadius = ScreenX375(8);
    shadowBg.layer.shadowColor = RGBACOLOR(0, 0, 0, 0.11).CGColor;
    shadowBg.layer.shadowOffset = CGSizeMake(0, 4);
    shadowBg.layer.shadowOpacity = 1;
    shadowBg.layer.shadowRadius = 12;
    [self addSubview:shadowBg];
    
    UIView *allBg = [[UIView alloc] initWithFrame:CGRectMake(ScreenX375(19), ScreenX375(12), (SCREEN_WIDTH - ScreenX375(38)), ScreenX375(407))];
    allBg.backgroundColor = [UIColor whiteColor];
    allBg.layer.cornerRadius = ScreenX375(8);
    allBg.clipsToBounds = YES;
    [self addSubview:allBg];
    
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(18), ScreenX375(18), ScreenX375(150), ScreenX375(28))];
    self.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:24];
    self.titleLabel.textColor = REXADECIMALCOLOR(@"#212121");
    [allBg addSubview:self.titleLabel];
    
    self.noteLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(18), ScreenX375(53), ScreenX375(150), ScreenX375(14))];
    self.noteLabel.textColor = REXADECIMALCOLOR(@"#888888");
    self.noteLabel.font = Kfont(12);
    [allBg addSubview:self.noteLabel];
    
    self.tagView = [[UIView alloc] initWithFrame:CGRectMake(ScreenX375(125), ScreenX375(53), ScreenX375(150), ScreenX375(14))];
    [allBg addSubview:self.tagView];
    
    UIImageView *tagIcon = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenX375(12), ScreenX375(14))];
    tagIcon.image = ImageWithName(@"尺子_o");
    [self.tagView addSubview:tagIcon];
    
    self.tagLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(15), 0, ScreenX375(100), ScreenX375(14))];
    self.tagLabel.textColor = RGB_COLOR(103, 162, 231);
    self.tagLabel.font = Kfont(12);
    self.tagLabel.text = @"女·中年·夏·套装";
    [self.tagView addSubview:self.tagLabel];
    
    self.concernBtn = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - ScreenX375(120), ScreenX375(21), ScreenX375(64), ScreenX375(26))];
    [self.concernBtn setImage:ImageWithName(@"添加关注") forState:UIControlStateNormal];
    [self.concernBtn setTitle:@" 关注" forState:UIControlStateNormal];
    self.concernBtn.titleLabel.font = Kfont(14);
    [self.concernBtn setTitleColor:REXADECIMALCOLOR(@"#ff2469") forState:UIControlStateNormal];
    self.concernBtn.layer.cornerRadius = ScreenX375(13);
    self.concernBtn.layer.borderWidth = 1;
    self.concernBtn.layer.borderColor = REXADECIMALCOLOR(@"#eaeaea").CGColor;
    [self.concernBtn addTarget:self action:@selector(concernAction) forControlEvents:UIControlEventTouchUpInside];
    [allBg addSubview:self.concernBtn];
    
    self.headImage = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenX375(18), ScreenX375(86), SCREEN_WIDTH - ScreenX375(74), ScreenX375(301))];
    self.headImage.layer.cornerRadius = ScreenX375(8);
    self.headImage.clipsToBounds = YES;
    self.headImage.image = ImageWithName(@"1");
    [allBg addSubview:self.headImage];
    
    self.numLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(35), ScreenX375(346), ScreenX375(200), ScreenX375(26))];
    self.numLabel.text = @"作品数 789  |  粉丝数 789";
    self.numLabel.backgroundColor = RGBACOLOR(255, 255, 255, 0.8);
    self.numLabel.layer.cornerRadius = ScreenX375(13);
    self.numLabel.clipsToBounds = YES;
    self.numLabel.font = Kfont(13);
    self.numLabel.textAlignment = NSTextAlignmentCenter;
    [allBg addSubview:self.numLabel];
    
    self.videoBtn = [[UIButton alloc] initWithFrame:CGRectMake(ScreenX375(144), ScreenX375(212), ScreenX375(50), ScreenX375(50))];
    [self.videoBtn setImage:ImageWithName(@"Group 9") forState:UIControlStateNormal];
    [self.videoBtn addTarget:self action:@selector(playVideo) forControlEvents:UIControlEventTouchUpInside];
    self.videoBtn.hidden = YES;
    [allBg addSubview:self.videoBtn];
}

- (void)playVideo {
    if (_playClick) {
        _playClick();
    }
}

- (void)setChannelModel:(ChannelModel *)channelModel {
    _channelModel = channelModel;
    self.titleLabel.text = channelModel.name;
    self.noteLabel.text = channelModel.intro;
    CGFloat noteLenth = kGetStringWidth(channelModel.intro, CGSizeMake(ScreenX375(150), ScreenX375(14)), Kfont(12));
    self.tagView.frame = CGRectMake(ScreenX375(32) + noteLenth, ScreenX375(53), ScreenX375(150), ScreenX375(14));
    [self.headImage sd_setImageWithURL:[NSURL URLWithString:channelModel.pic] placeholderImage:nil options:SDWebImageRetryFailed];
    self.tagLabel.text = [NSString stringWithFormat:@"%@·%@·%@·%@",channelModel.sex,channelModel.ages,channelModel.quarter,channelModel.type];
    self.numLabel.text = [NSString stringWithFormat:@"作品数 %@  |  粉丝数 %@",channelModel.proNum,channelModel.fensNum];
    
    if ([channelModel.isFollow integerValue]) {
        [self.concernBtn setTitle:@"已关注" forState:UIControlStateNormal];
        [self.concernBtn setTitleColor:REXADECIMALCOLOR(@"#212121") forState:UIControlStateNormal];
        [self.concernBtn setImage:nil forState:UIControlStateNormal];
    }else {
        [self.concernBtn setTitle:@" 关注" forState:UIControlStateNormal];
        [self.concernBtn setTitleColor:REXADECIMALCOLOR(@"#ff2469") forState:UIControlStateNormal];
        [self.concernBtn setImage:ImageWithName(@"添加关注") forState:UIControlStateNormal];
    }
    
    if ([channelModel.pic containsString:@"png"] || [channelModel.pic containsString:@"jpg"]) {
        self.videoBtn.hidden = YES;
    }else {
        self.videoBtn.hidden = NO;
    }
}

- (void)concernAction {
    if (_concernClick) {
        _concernClick();
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

@implementation ChannelModel

+(JSONKeyMapper*)keyMapper
{
    return [[JSONKeyMapper alloc] initWithModelToJSONDictionary:@{
                                                                  @"Id": @"id"
                                                                  }];
}

@end

